* Regression for shooting-related news coverage on fatalities

clear all
set more off

cap mkdir "results"
cap mkdir "results/fig"

use "out-data/gun_news.dta", clear

gen byte shoot = fatalities != 0 if !mi(fatalities)
la var shoot "Mass shooting"

gen byte month = month(date)
la var month "Month"

eststo clear

reg logdur shoot L.shoot, robust
eststo m0, title("Shoot")

reg logdur L(0/10).shoot, robust
eststo m1, title("Shoot Lags")

reg logdur L(0/10).shoot i.dow, robust
eststo m2, title("Shoot Lags DoW")

reg logdur L(0/10).shoot i.dow i.month i.year, robust
eststo m3, title("Shoot Lags DoW/Year")

reg logdur F(1/10).shoot L(0/10).shoot i.dow i.month i.year, robust
matrix S = r(table)
eststo m4, title("Shoot Leads")

reg logdur fatalities L.fatalities, robust
eststo m5, title("Fatal")

reg logdur L(0/10).fatalities, robust
eststo m6, title("Fatal Lags")

reg logdur L(0/10).fatalities i.dow, robust
eststo m7, title("Fatal Lags DoW")

reg logdur L(0/10).fatalities i.dow i.month i.year, robust
eststo m8, title("Fatal Lags DoW/Year")

reg logdur F(1/10).fatalities L(0/10).fatalities i.dow i.month i.year, robust
matrix B = r(table)
eststo m9, title("Fatal Leads")

esttab using "results/reg_news.csv", replace nogap noomit label nodep     /*
    */ drop(*.month *.dow *.year) varwidth(20) b(3) se(3) parentheses     /*
    */ star(* .10 ** .05 *** .01) stardetach stats(N, fmt(0) labels("N")) /*
    */ order(shoot L*shoot fatalities L*fatalities F*shoot F*fatalities)


// clean up column names so they are valid variable names
loc cn: colnames S
loc cn_clean ""
foreach k of local cn {
    loc nname = lower(subinstr("`k'", ".", "_", .))
    loc cn_clean "`cn_clean' bs`nname'"
}
matrix colnames S = `cn_clean'

loc cn: colnames B
loc cn_clean ""
foreach k of local cn {
    loc nname = lower(subinstr("`k'", ".", "_", .))
    loc cn_clean "`cn_clean' bf`nname'"
}
matrix colnames B = `cn_clean'

matrix A = S, B

clear
qui svmat2 A, names(col) rnames(stat) full

qui reshape long b, i(stat) j(varname) string
qui reshape wide b, i(varname) j(stat) string
foreach v of varlist _all {
    loc nname = regexr("`v'", "^b", "")
    qui rename `v' `nname'
}

// only interested in shooting and fatality coefficients
keep if regexm(varname, "fatalities") == 1 | regexm(varname, "shoot") == 1

// get coefficient relative to shooting time
split varname, gen(v) parse("_")
replace v1 = regexr(v1, "^[sf]", "")
gen time = regexs(1) if regexm(v1, "[fl]([0-9]*)") == 1
destring time, replace
replace time = 1 if inlist(v1, "l", "f")
replace time = 0 if inlist(v1, "fatalities", "shoot")
replace time = time * -1 if substr(v1, 1, 1) == "f"

gen byte kind = regexm(varname, "fatalities")
cap la drop vk
la def vk 0 "Shoot" 1 "Fatalities"
la val kind vk

keeporder kind time b se ll ul pvalue
rename (ll ul) (lb95 ub95)
sort kind time

// graph coefficients
loc gstyle "grid glwidth(vthin) glcolor(gs14) glpattern(shortdash)"

twoway (connected b time, msize(small) mcolor(black) lcolor(black)) /*
    */ (line lb ub time, lcolor(black black) lpattern(dash dash) lwidth(thin thin)) /*
    */ if kind == 0 /*
    */ , yline(0, lpattern(solid) lcolor(gs10)) /*
    */   ylabel(, labsize(medsmall) `gstyle') /*
    */   ytitle("Coefficient on Shooting Indicator", size(medsmall) height(5)) /*
    */   xline(0, lpattern(solid) lcolor(gs10)) /*
    */   xlabel(, labsize(medsmall) `gstyle') /*
    */   xtitle("Days Relative to Shooting", size(medsmall) height(5)) /*
    */   graphregion(color(white)) legend(off)

graph export "results/fig/shoot_coefficients.pdf", replace

twoway (connected b time, msize(small) mcolor(black) lcolor(black)) /*
    */ (line lb ub time, lcolor(black black) lpattern(dash dash) lwidth(thin thin)) /*
    */ if kind == 1 /*
    */ , yline(0, lpattern(solid) lcolor(gs10)) /*
    */   ylabel(, labsize(medsmall) `gstyle') /*
    */   ytitle("Coefficient on Fatalities", size(medsmall) height(5)) /*
    */   xline(0, lpattern(solid) lcolor(gs10)) /*
    */   xlabel(, labsize(medsmall) `gstyle') /*
    */   xtitle("Days Relative to Shooting", size(medsmall) height(5)) /*
    */   graphregion(color(white)) legend(off)

graph export "results/fig/fatality_coefficients.pdf", replace
exit
